<template>
  <div>
      <Breadcard name1="商品管理" name2="添加商品"/>
      <el-card>
           <el-alert title="添加商品信息" center type="info" show-icon :closable="false"></el-alert>
          <el-steps :space="200" :active="activeStep - 0" finish-status="success" align-center="">
            <el-step title="基本信息"></el-step>
            <el-step title="商品参数"></el-step>
            <el-step title="商品属性"></el-step>
            <el-step title="商品图片"></el-step>
            <el-step title="商品内容"></el-step>
            <el-step title="完成"></el-step>
          </el-steps>
          <el-form :model="addForm" status-icon :rules="goodsFormRule" ref="goodsFormRef" label-width="100px" label-position="top">
          <el-tabs v-model="activeStep" :tab-position="'left'" :before-leave="beforeTabsleave" @tab-click="tabsclick">
                <el-tab-pane label="基本信息" name="0">
                    <el-form-item label="商品名称:" prop="goods_name">
                        <el-input v-model="addForm.goods_name"></el-input>
                    </el-form-item>
                    <el-form-item label="商品价格:" prop="goods_price">
                        <el-input v-model="addForm.goods_price" type="number"></el-input>
                    </el-form-item>
                    <el-form-item label="商品重量:" prop="goods_weight">
                        <el-input v-model="addForm.goods_weight" type="number"></el-input>
                    </el-form-item>
                    <el-form-item label="商品数量:" prop="goods_number">
                        <el-input v-model="addForm.goods_number" type="number"></el-input>
                    </el-form-item>
                    <el-form-item label="商品分类:" prop="goods_cat">
                        <el-cascader
                        v-model="addForm.goods_cat"
                        :options="classList"
                        :props="{ expandTrigger: 'hover', ...goodsProps}"
                        @change="handleChange"></el-cascader>
                    </el-form-item>
                </el-tab-pane>
                <el-tab-pane label="商品参数" name="1">商品参数</el-tab-pane>
                <el-tab-pane label="商品属性" name="2">商品属性</el-tab-pane>
                <el-tab-pane label="商品图片" name="3">商品图片</el-tab-pane>
                <el-tab-pane label="商品内容" name="4">商品内容</el-tab-pane>
          </el-tabs>
          </el-form>
      </el-card>
  </div>
</template>

<script>
import Breadcard from '../../components/Breadcard/Breadcard.vue'
export default {
    components:{
        Breadcard
    },
    data () {
        return {
            activeStep:0,
            //添加的商品表单
            addForm:{
                goods_price:0,
                goods_name:'',
                goods_weight:0,
                goods_number:0,
                //商品所属的分类数组
                goods_cat :[]
            },
            goodsFormRule:{
                goods_name:[
                    {required:true,message:'请输入商品名称',trigger:'blur'}
                ],
                goods_price:[
                    {required:true,message:'请输入商品价格',trigger:'blur'}
                ],
                goods_number:[
                    {required:true,message:'请输入商品数量 ',trigger:'blur'}
                ],
                goods_weight:[
                    {required:true,message:'请输入商品重量',trigger:'blur'}
                ],
                goods_cat:[
                    {required:true,message:'请选择商品分类',trigger:'blur'}
                ]
            },
            goodsProps:{
                lebale:'cat_name',
                value:'cat_id',
                children:'children'
            },
            classList:[],
            //动态参数列表
            manyTabeldata:[]
        }
    },
    created () {
        this.getClassList()
    },
    methods: {
        async getClassList(){
            const {data:res} = await this.$http.get('categories')
            if(res.meta.status !== 200){
                return this.$message.error('获取商品分类失败')
            }
            this.classList = res.data
            
        },
        handleChange(){

        },
        beforeTabsleave(activeName,oldactiveName){
            if(oldactiveName === '0' && this.addForm.goods_cat.length !== 3){
                return false
            }
            
        },
        async tabsclick(){
            if(this.activeStep === '1'){
                const {data:res} = await this.$http.get(`categories/${this.classId}/attributes`,{
                    params:{sel:'many'}
                })
                if(res.meta.status !== 200){
                    return this.$message.error('获取商品属性参数失败')
                }
                this.$message.success('获取商品属性参数成功')
                console.log(res.data)
                this.manyTabeldata = res.data
            }
            
        }
    },
    computed:{
        classId(){
            if(this.addForm.goods_cat.length === '3'){
                return this.addForm.goods_cat[2]
            }
            return null
        }
    }

}
</script>

<style lang="less" scoped>
.el-steps{
    margin: 15px 0;
    font-size: 13px;
}
.el-cascader-menu{
    height: 200px;
}

</style>